package com.cnc.tag;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

/**
 * 从请求中获取 dataList 数据 ，并输出为 select的 option 选项<br>
 * Example:<br>
 * <html:select property="tfAccount.accountTypeCode"
 * onchange="fillBizTypeApp(this);fillPermits(this)" styleId="accountTypeCode">
 * <custom:options dataList = "dataList" label="accountTypeName"
 * value="accountTypeCode" blank="true" blankLabel="=========请 选 择========="></custom:options>
 * </html:select>
 */
public class OptionsTag extends LtcmTag {
	/**
	 * list存放在请求属性中的名称
	 */
	private String dataList = "dataList";

	/**
	 * list 存放的位置 page,request,session,application
	 */
	private String scope = "page";

	/**
	 * list中一个对象的属性来产生option的value值
	 */
	private String value = "code";

	/**
	 * list中一个对象的属性来产生option的label值
	 */
	private String label = "name";

	/**
	 * 是否产生一个空的选项
	 */
	private boolean blank = false;

	/**
	 * 空选择项的label
	 */
	private String blankLabel = "";

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public boolean isBlank() {
		return blank;
	}

	public void setBlank(boolean blank) {
		this.blank = blank;
	}

	public String getDataList() {
		return dataList;
	}

	public void setDataList(String dataList) {
		this.dataList = dataList;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getBlankLabel() {
		return blankLabel;
	}

	public void setBlankLabel(String blankLabel) {
		this.blankLabel = blankLabel;
	}

	public int doEndTag() throws JspException {
		List lstDataList;
		if ("page".equalsIgnoreCase(scope)) {
			lstDataList = (List) pageContext.getAttribute(dataList);
		} else if ("request".equalsIgnoreCase(scope)) {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			lstDataList = (List) request.getAttribute(this.dataList);
		} else if ("session".equalsIgnoreCase(scope)) {
			lstDataList = (List) pageContext.getSession().getAttribute(dataList);
		} else {
			lstDataList = (List) pageContext.getServletContext().getAttribute(dataList);
		}
		lstDataList = lstDataList == null ? new ArrayList() : lstDataList;
		Iterator iter = lstDataList.iterator();
		StringBuffer results = new StringBuffer();
		if (blank) {
			results.append("<option value=\"\">").append(blankLabel).append("</option>");
		}
		Object valueObj;
		Object labelObj;
		String valueVal;
		String labelVal;
		while (iter.hasNext()) {
			Object node = iter.next();
			if (node instanceof HashMap) {
				valueObj = ((HashMap) node).get(value);
				labelObj = ((HashMap) node).get(label);
			} else {
				try {
					valueObj = PropertyUtils.getSimpleProperty(node, value);
					labelObj = PropertyUtils.getSimpleProperty(node, label);
				} catch (Exception e) {
					valueObj = null;
					labelObj = null;
				}
			}
			valueVal = (valueObj == null ? "" : valueObj.toString().trim());
			labelVal = (labelObj == null ? "" : labelObj.toString().trim());
			results.append("<option value=\"");
			results.append(valueVal);
			results.append("\" ");
			SelectTag selecttag = this.selectTag();
			if (selecttag != null) {
				if (selecttag.isMatched(valueVal)) {
					results.append(" selected=\"selected\"");
				}
			}
			results.append(">");
			results.append(labelVal);
			results.append("</option>");
		}
		ResponseUtils.write(pageContext, results.toString());
		dataList = "dataList";
		scope = "page";
		value = "code";
		label = "name";
		blank = false;
		blankLabel = "";
		return (EVAL_PAGE);
	}

	private SelectTag selectTag() {
		return (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
	}

}
